var batteryChargeLogTable = function() {
	var validator;
	return {
		init : function() {
			batteryChargeLogTable._initShortcutOfDate();
			var tableId = "charge_log_table";
			util.initCheckBox(tableId);
			var checkBoxDivStr = '<div class="checker" onclick="util.selectCheckBox(\'' + tableId + '\', this)"><span><input type="checkbox" class="checkboxes"></span></div>';

			//查询字典信息列表
			function queryData(sSource, aoData, fnCallback) {
				aoData.push({
					"name" : "customer",
					"value" : $("#id_cusName").val().trim()
				});
				aoData.push({
					"name" : "packCode",
					"value" : $("#id_packCode").find('option:selected').text().trim()
				});
				aoData.push({
					"name" : "gatherTimeStart",
					"value" : $("#gatherTime_start").val().trim()
				});
				aoData.push({
					"name" : "gatherTimeEnd",
					"value" : $("#gatherTime_end").val().trim()
				});
				$.ajax({
					"type" : "post",
					"url" : sSource,
					"dataType" : "json",
					"data" : {
						"aoData" : JSON.stringify(aoData)
					},
					"success" : function(resp) {
						fnCallback(resp);
					}
				});
			};

			$('#charge_log_table').DataTable({
				"processing" : true,
				"serverSide" : true,
				"sAjaxSource" : "main/batteryLog/getBatteryChargeLog2.mvc",
				"fnServerData" : queryData,
				"pageLength" : 10,
				"sDom" : "<'row-fluid'r>t<'row-fluid'<'span6'i><'span6'p>>",
				"oLanguage" : util.getDToLanguage(),
				"columns" : [ 
				{	
					"data":"index",//序号
					"bSortable" : false,
            		"render":function(data,type,row,meta){
            			var startIndex = meta.settings._iDisplayStart;
             			return startIndex+meta.row+1;
         			}
				},
				{
					"data" : "packCode",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"data" : "colClientId",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"data" : "cusName",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"data" : "startTime",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"data" : "endTime",
					"bSortable" : false,
					"defaultContent": ""
				},
				{
					"data" : "totalChargeTime",
					"bSortable" : false,
					"render" : function(data, type, full, meta) {
						if(data>0){
							var _hour = parseInt(data/3600);
							var _minutes = parseInt((data%3600)/60);
							var _seconds = parseInt((data%3600)%60);
							return _hour+'小时'+_minutes+'分'+_seconds+'秒';
						}else {
							return '';
						}
						
					}
				},
				{
					"data" : "socStart",
					"bSortable" : false,
					"render" : function(data, type, full, meta) {
						if(data){
							return data+'%';
						}else {
							return '';
						}
					}
				},
				{
					"data" : "socEnd",
					"bSortable" : false,
					"render" : function(data, type, full, meta) {
						if(data){
							return data+'%';
						}else {
							return '';
						}
					}
				}
				],
				"fnDrawCallback" : function(oSetting) {
					util.clearSelected(tableId);
				}
			});

			$("#chargelog_table_exp").on('click',function(e){
				var aoData=new Array();
				aoData.push({
					"name" : "customer",
					"value" : $("#id_cusName").val().trim()
				});
				aoData.push({
					"name" : "packCode",
					"value" : $("#id_packCode").find('option:selected').text().trim()
				});
				aoData.push({
					"name" : "gatherTimeStart",
					"value" : $("#gatherTime_start").val().trim()
				});
				aoData.push({
					"name" : "gatherTimeEnd",
					"value" : $("#gatherTime_end").val().trim()
				});
				
				$.fileDownload('main/batteryLog/exportBatteryChargeLog.mvc', {
	    			httpMethod: 'POST',
	    			data: {"aoData":JSON.stringify(aoData)},
	    			prepareCallback:function(url){
	    				
	    			},
	    			successCallback:function(url){
	    				//App.unblockUI($(loadingId));//停止loading
	    				rmsBox.notify('success', {msg: '操作成功'});
	    			},
	    			failCallback: function (html, url) {
	    				//App.unblockUI($(loadingId));//停止loading
	    				rmsBox.notify('error', {msg: '操作失败！',delay:3000});
	    			}
            	});
				
				
			});
			// 查询
			$("#btn_query").click(function(e) {
				var table = $('#charge_log_table').DataTable();
				table.draw(true);
			});

			//重置查询条件
			$("#btn_reset").click(function(e) {
				$("#customer_search_form")[0].reset();
				$(".select2-selection__rendered").empty();
				$(".select2-selection__rendered").append("<span class='select2-selection__placeholder'>请选择</span>");
			});
			
			$(function(){
				
				util.initCustomerSelect("id_cusName","220px");
				util.initPackCodeSelect("id_packCode","220px");
				$('.search-body input').bind('keyup', function(event) {
					if (event.keyCode == "13") {
						//回车执行查询
						$('.search-button #btn_query').click();
					}
				});
	
			});


			
		},

		_initShortcutOfDate : function() {

			$("#shortcut_time").on('change', function(e) {
				var _dateType = $("#shortcut_time").val();
				$.ajax({
					"type": "post",
					"url": "main/realtimeBoard/getShortcutOfDate.mvc",
					"async": true,
					"dataType": "json",
					"success": function(res) {
						if (_dateType == '0') {
							$("#gatherTime_start").val(res.yesterdayStart);
							$("#gatherTime_end").val(res.yesterdayEnd);
						} else if (_dateType == '1') {
							$("#gatherTime_start").val(res.beginDayOfLastWeek);
							$("#gatherTime_end").val(res.endDayOfLastWeek);
						} else if (_dateType == '2') {
							$("#gatherTime_start").val(res.beginDayOfLastMonth);
							$("#gatherTime_end").val(res.endDayOfLastMonth);
						} else if (_dateType == '3') {
							$("#gatherTime_start").val(res.beginOfLastQuarter);
							$("#gatherTime_end").val(res.endOfLastQuarter);
						} else if (_dateType == '4') {
							$("#gatherTime_start").val(res.beginOfLastYear);
							$("#gatherTime_end").val(res.endOfLastYear);
						}  else if (_dateType == '5') {
							$("#gatherTime_start").val('');
							$("#gatherTime_end").val('');
						}
					}
				});
			});
			
		}
	};
}();